﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;//引入命名空间，绘制带箭头的直线
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _10_GDI__绘制
{
    public partial class Form1 : Form
    {
        //画布
        Graphics g;
        //矩形区域
        Rectangle rectangle;

        public Form1()
        {
            InitializeComponent();
            //场景画布区域(指定在什么位置绘制)
            g = this.panel1.CreateGraphics();
            //减少毛边：抗锯齿
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //线段平滑: 高质量插值法
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        }
        /// <summary>
        /// 绘制直线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //在画布上绘制直线
            Pen pen = new Pen(Color.SeaGreen, 2);

            //参数1：画笔(画笔颜色,画笔宽度)
            //参数2：起始点坐标X坐标
            //参数3：起始点坐标Y坐标
            //参数4：结束点坐标X坐标
            //参数5：结束点坐标Y坐标
            g.DrawLine(pen, 100, 100, 500, 100);
        }
        /// <summary>
        /// 绘制带箭头的直线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //笔
            Pen pen = new Pen(Color.Red, 2);
            //箭头
            //参数1：箭头横坐标长度
            //参数2：箭头纵坐标长度
            //参数3：是否封闭
            AdjustableArrowCap lineCap = new AdjustableArrowCap(5, 5, true);

            //设置线帽(直线终点)
            pen.CustomEndCap = lineCap;

            //绘制带箭头的直线
            g.DrawLine(pen, 100, 100, 500, 100);

        }

        /// <summary>
        /// 绘制带箭头的虚线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            Pen pen = new Pen(Color.Red, 4);
            //箭头
            AdjustableArrowCap lineCap = new AdjustableArrowCap(5, 5, true);
            pen.CustomEndCap = lineCap;

            //设置笔为:虚线
            pen.DashStyle = DashStyle.Dash;
            //绘制带箭头的虚线 （线条样式）
            g.DrawLine(pen, 100, 100, 500, 100);

            //设置笔为:实线
            pen.DashStyle = DashStyle.Solid;
            //绘制带箭头的虚线 （线条样式）
            g.DrawLine(pen, 100, 150, 500, 150);


            //设置笔为:点线
            pen.DashStyle = DashStyle.DashDot;
            //绘制带箭头的虚线 （线条样式）
            g.DrawLine(pen, 100, 200, 500, 200);

            //设置笔为:点点线
            pen.DashStyle = DashStyle.DashDotDot;
            //绘制带箭头的虚线 （线条样式）
            g.DrawLine(pen, 100, 250, 500, 250);

            //设置笔为:点点线
            pen.DashStyle = DashStyle.Dot;
            //绘制带箭头的虚线 （线条样式）
            g.DrawLine(pen, 100, 300, 500, 300);

            //设置笔:自定义
            pen.DashStyle = DashStyle.Custom;
            //DashPattern：实数数组，指定虚线中交替出现的短划线和空白区域的长度。
            pen.DashPattern = new float[] { 10, 5, 66, 5 };
            //绘制带箭头的虚线 （线条样式）
            g.DrawLine(pen, 100, 350, 500, 350);


        }
        /// <summary>
        /// 绘制坐标轴
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            Pen pen = new Pen(Color.Black, 3);
            //设置结束点箭头（自定义的）
            pen.CustomEndCap = new AdjustableArrowCap(5, 5, true);

            //绘制X轴
            g.DrawLine(pen, 0, this.panel1.Height / 2, this.panel1.Width, this.panel1.Height / 2);
            //绘制Y轴
            g.DrawLine(pen,  this.panel1.Width / 2, this.panel1.Height,this.panel1.Width / 2, 0);

        }
        /// <summary>
        /// 多点连接成线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            //多点连接成线
            //参数一: 画笔
            //参数二: 点的集合
            g.DrawLines(new Pen(Color.DarkGray, 3) { CustomEndCap = new AdjustableArrowCap(5, 5, true) }, new Point[] 
            {
                new Point(100,100),new Point(100,200),
                new Point(200,200),new Point(200,300)
            });

            //将上述的点绘制出来，方便观察
            //参数一: 画笔颜色
            //参数二: 矩形左上角X坐标
            //参数三: 矩形左上角Y坐标
            //参数四: 矩形宽度
            //参数五: 矩形高度
            g.FillEllipse(Brushes.Red, 100-5, 100-5, 10, 10);
            g.FillEllipse(Brushes.Red, 100 - 5, 200 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 200 - 5, 200 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 200 - 5, 300 - 5, 10, 10);

        }
        /// <summary>
        /// 绘制曲线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {

            //绘制曲线
            g.DrawCurve(new Pen(Color.DarkGray, 3), new Point[]
            {
                new Point(100,100),new Point(200,200),
                new Point(300,100),new Point(400,200),
                new Point(500,100),new Point(600,200),
            });

            //将上述的点绘制出来，方便观察
            g.FillEllipse(Brushes.Red, 100 - 5, 100 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 200 - 5, 200 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 300 - 5, 100 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 400 - 5, 200 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 500 - 5, 100 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 600 - 5, 200 - 5, 10, 10);

        }

        private void button7_Click(object sender, EventArgs e)
        {
            //绘制闭合曲线
            g.DrawClosedCurve(new Pen(Color.DarkGray, 3), new Point[]
            {
                new Point(100,100),new Point(200,200),
                new Point(300,100),new Point(400,200),
                new Point(500,100),new Point(600,200),
            });

            //将上述的点绘制出来，方便观察
            g.FillEllipse(Brushes.Red, 100 - 5, 100 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 200 - 5, 200 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 300 - 5, 100 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 400 - 5, 200 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 500 - 5, 100 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 600 - 5, 200 - 5, 10, 10);
        }
        /// <summary>
        /// 绘制矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            //绘制矩形
            //参数一: 画笔
            //参数二: 矩形左上角X坐标
            //参数三: 矩形左上角Y坐标
            //参数四: 矩形宽度
            //参数五: 矩形高度
            g.DrawRectangle(new Pen(Color.DarkGray, 3), 100, 100, 200, 200);
        }
        /// <summary>
        /// 绘制多个矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            g.DrawRectangles(new Pen(Color.DarkGray, 3), new Rectangle[]
            {
                new Rectangle(100,100,100,100),
                new Rectangle(200,200,100,100),
                new Rectangle(300,300,100,100)
            });
        }
        /// <summary>
        /// DrawPolygon绘制多边型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button10_Click(object sender, EventArgs e)
        {
            //绘制多边型:六边形
            g.DrawPolygon(new Pen(Color.DarkGray, 3),new Point[]
            {
                new Point(100,100),new Point(200,100),
                new Point(250,150),new Point(200,200),
                new Point(100,200),new Point(50,150)
            });

            //绘制点方便观察
            g.FillEllipse(Brushes.Red, 100 - 5, 100 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 200 - 5, 100 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 250 - 5, 150 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 200 - 5, 200 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 100 - 5, 200 - 5, 10, 10);
            g.FillEllipse(Brushes.Red, 50 - 5, 150 - 5, 10, 10);

        }
        /// <summary>
        /// 绘制椭圆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button11_Click(object sender, EventArgs e)
        {
            //绘制奥运五环
            //蓝色
            g.DrawEllipse(new Pen(Color.Blue, 3), 100, 100, 100, 100);
            //黄色
            g.DrawEllipse(new Pen(Color.Yellow, 3), 150, 150, 100, 100);
            //黑色
            g.DrawEllipse(new Pen(Color.Black, 3), 200, 100, 100, 100);
            //绿色
            g.DrawEllipse(new Pen(Color.Green, 3), 250, 150, 100, 100);
            //红色
            g.DrawEllipse(new Pen(Color.Red, 3), 300, 100, 100, 100);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            //笔
            Pen pen = new Pen(Color.SeaGreen, 1);
            //绘制线段为虚线
            pen.DashStyle = DashStyle.Dash;
            //绘制矩形=>作为参考，辅助理解
            g.DrawRectangle(pen, 100, 100, 200, 200);

            //绘制扇形
            //参数一: 画笔
            //参数二: 矩形左上角X坐标
            //参数三: 矩形左上角Y坐标
            //参数四: 矩形宽度
            //参数五: 矩形高度
            //参数六: 起始角度
            //参数七: 扫过的角度
            g.DrawPie(new Pen(Color.DarkGray, 3), 100, 100, 200, 200, 180, -90);
        }
        /// <summary>
        /// DrawPath(绘制路径)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button13_Click(object sender, EventArgs e)
        {
            //设置绘制的路径
            GraphicsPath path = new GraphicsPath();
            //添加圆
            path.AddEllipse(100, 100, 200, 200);
            //添加矩形
            path.AddRectangle(new Rectangle(150, 150, 100, 100));

            //绘制路径
            g.DrawPath(new Pen(Color.DarkGray, 3), path);
        }
        /// <summary>
        /// 绘制圆角矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button14_Click(object sender, EventArgs e)
        {
            Pen pen = new Pen(Color.Orange, 2);

            //绘制矩形
            g.DrawRectangle(pen, 50, 100, 400, 200);

            //圆的半径
            int radius = 50;

            #region 左上角

            //左上角,绘制矩形
            Pen pen1 = new Pen(Color.SeaGreen, 2);
            //绘制线段为虚线
            pen1.DashStyle = DashStyle.Dash;
            //绘制矩形
            g.DrawRectangle(pen1, 50, 100, radius, radius);
            //绘制矩形中心点的垂线
            g.DrawLine(pen1, 50 + radius / 2, 100, 50 + radius / 2, 100 + radius);
            g.DrawLine(pen1, 50, 100 + radius / 2, 50 + radius, 100 + radius / 2);

            //绘制左上角的圆角
            g.DrawArc(pen, 50, 100, radius, radius, 180, 90);

            #endregion


            #region 右上角

            //绘制矩形
            g.DrawRectangle(pen1, 450 - radius, 100, radius, radius);
            //绘制矩形中心点的垂线
            g.DrawLine(pen1, 450 - radius / 2, 100, 450 - radius / 2, 100 + radius);
            g.DrawLine(pen1, 450 - radius, 100 + radius / 2, 450, 100 + radius / 2);

            //绘制右上角的圆角
            g.DrawArc(pen, 450 - radius, 100, radius, radius, 270, 90);

            #endregion

            #region 左下角

            //绘制矩形
            g.DrawRectangle(pen1, 50, 300 - radius, radius, radius);
            //绘制矩形中心点的垂线
            g.DrawLine(pen1, 50 + radius / 2, 300 - radius, 50 + radius / 2, 300);
            g.DrawLine(pen1, 50, 300 - radius / 2, 50 + radius, 300 - radius / 2);

            //绘制左下角的圆角
            g.DrawArc(pen, 50, 300 - radius, radius, radius, 90, 90);

            #endregion

            #region 右下角

            //绘制矩形
            g.DrawRectangle(pen1, 450 - radius, 300 - radius, radius, radius);
            //绘制矩形中心点的垂线
            g.DrawLine(pen1, 450 - radius / 2, 300 - radius, 450 - radius / 2, 300);
            g.DrawLine(pen1, 450 - radius, 300 - radius / 2, 450, 300 - radius / 2);

            //绘制右下角的圆角
            g.DrawArc(pen, 450 - radius, 300 - radius, radius, radius, 0, 90);

            #endregion


        }
        /// <summary>
        /// 绘制圆角矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button15_Click(object sender, EventArgs e)
        {
            //圆的半径
            int radius = 50;


            //构建路径
            GraphicsPath path = new GraphicsPath();

            //左上角
            path.AddArc(50, 100, radius, radius, 180, 90);
            //上方直线
            path.AddLine(50 + radius, 100, 450 - radius, 100);
            //右上角
            path.AddArc(450 - radius, 100, radius, radius, 270, 90);
            //右边直线
            path.AddLine(450, 100 + radius, 450, 300 - radius);
            //右下角
            path.AddArc(450 - radius, 300 - radius, radius, radius, 0, 90);
            //下方直线
            path.AddLine(450 - radius, 300, 50 + radius, 300);
            //左下角
            path.AddArc(50, 300 - radius, radius, radius, 90, 90);
            //左边直线
            path.AddLine(50, 300 - radius, 50, 100+ radius/2);

            //填充路径(封闭形成形状，然后形象填充颜色)
            g.FillPath(new SolidBrush(Color.SeaGreen), path);
            //绘制边框
            g.DrawPath(new Pen(Color.Gray, 6), path);

        }
        /// <summary>
        /// 绘制字体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button16_Click(object sender, EventArgs e)
        {
            //绘制文本
            g.DrawString("Hello World",new Font("微软雅黑",20),new SolidBrush(Color.Red),new Point(100,100));
        }

        private void button17_Click(object sender, EventArgs e)
        {
            //构建指定的绘制区域
            Rectangle rect = new Rectangle(100, 100, 300, 64);
            //画出绘制字体的区域
            g.DrawRectangle(new Pen(Color.DarkGray, 2), rect);

            //规定字体的格式=>水平与垂直居中
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;//水平居中
            format.LineAlignment = StringAlignment.Center;//垂直居中

            //在指定的区域内绘制字体
            g.DrawString("Hello World", new Font("微软雅黑", 20), new SolidBrush(Color.Red), rect, format);

            //在下方绘制一个新矩形，用来显示文本的绘制区域，进行不同 对齐方式的对比
            Rectangle rectangle1 = new Rectangle(100, 164, 300, 64);
            g.DrawRectangle(new Pen(Color.DarkGray, 2), rectangle1);

            //左对齐
            format.Alignment = StringAlignment.Near;
            g.DrawString("Hello World", new Font("微软雅黑", 20), new SolidBrush(Color.Red), rectangle1, format);

            //在下方绘制一个新矩形，用来显示文本的绘制区域，进行不同 对齐方式的对比
            Rectangle rectangle2 = new Rectangle(100, 228, 300, 64);
            g.DrawRectangle(new Pen(Color.DarkGray, 2), rectangle2);

            //右对齐
            format.Alignment = StringAlignment.Far;
            g.DrawString("Hello World", new Font("微软雅黑", 20), new SolidBrush(Color.Red), rectangle2, format);




        }

        private void button18_Click(object sender, EventArgs e)
        {
            //读取指定路径下的图片到内存中
            Image Image = Image.FromFile(@"C:\Users\Admin\Pictures\捕获13.PNG");
            //获取图像的宽与高
            int width = Image.Width;
            int height = Image.Height;
            //绘制图像
            //参数一:图片
            //参数二:左上角X坐标
            //参数三:左上角Y坐标
            //参数四:宽度
            //参数五:高度
            g.DrawImage(Image, 100, 100, width, height);
        }
        /// <summary>
        /// GDI+绘制图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button19_Click(object sender, EventArgs e)
        {
            //创建绘制对象(给定宽高)
            Bitmap bitmap = new Bitmap(500, 500);
            //创建画布
            Graphics g = Graphics.FromImage(bitmap);
            //设置画布的属性：抗锯齿
            g.SmoothingMode = SmoothingMode.HighQuality;
            //设置画布的属性：线段平滑
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //绘制画布背景颜色为白色
            g.Clear(Color.White);//清空背景绘制成白色
            //绘制坐标轴
            g.DrawLine(new Pen(Color.Black, 2) { CustomEndCap = new AdjustableArrowCap(5, 5, true) },  250, 500,250, 0);
            g.DrawLine(new Pen(Color.Black, 2) { CustomEndCap = new AdjustableArrowCap(5, 5, true) },  0, 250,500, 250);
            //将图片写入到当前路径下保存
            bitmap.Save("./GDI+_绘制图片.jpg");
            //把图像绘制到画布容器上
            this.g.DrawImage(bitmap, 10, 10, 500, 500);
        }
        /// <summary>
        /// 填充椭圆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button20_Click(object sender, EventArgs e)
        {
            //填充椭圆
            //参数一: 画刷
            //参数二: 矩形左上角X坐标
            //参数三: 矩形左上角Y坐标
            //参数四: 矩形宽度
            //参数五: 矩形高度
            g.FillEllipse(new SolidBrush(Color.SeaGreen), 100, 100, 200, 200);
        }

        private void button21_Click(object sender, EventArgs e)
        {
            //填充矩形
            //参数一: 画刷
            //参数二: 矩形左上角X坐标
            //参数三: 矩形左上角Y坐标
            //参数四: 矩形宽度
            //参数五: 矩形高度
            g.FillRectangle(new SolidBrush(Color.SeaGreen), 100, 100, 200, 200);
        }
        /// <summary>
        /// 填充扇形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button22_Click(object sender, EventArgs e)
        {
            //填充扇形
            //参数一: 画刷
            //参数二: 矩形左上角X坐标
            //参数三: 矩形左上角Y坐标
            //参数四: 矩形宽度
            //参数五: 矩形高度
            //参数六: 起始角度
            //参数七: 扫过的角度
            g.FillPie(new SolidBrush(Color.SeaGreen), 100, 100, 200, 200, 180, -90);

        }
        /// <summary>
        /// 填充多边形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button23_Click(object sender, EventArgs e)
        {
            //定义一个五角星点的集合
            Point[] points = new Point[]
            {
                new Point(150, 50), new Point(180, 120),
                new Point(250, 120), new Point(195, 160),
                new Point(220, 230), new Point(150, 190),
                new Point(80, 230), new Point(105, 160),
                new Point(50, 120), new Point(120, 120)
            };
            //填充多边形
            g.FillPolygon(new SolidBrush(Color.SeaGreen), points);
        }

        private void button24_Click(object sender, EventArgs e)
        {
            //构建笔在画布上绘制X轴与Y轴
            Pen pen = new Pen(Color.DarkGray, 2);
            //设置结束点箭头（自定义的）
            pen.CustomEndCap = new AdjustableArrowCap(5, 5, true);
            //绘制X轴
            g.DrawLine(pen, 0, this.panel1.Height / 2, this.panel1.Width, this.panel1.Height / 2);
            //绘制Y轴
            g.DrawLine(pen, this.panel1.Width / 2, this.panel1.Height, this.panel1.Width / 2, 0);

            //设置一个矩形区域，以方便后续观察
            rectangle = new Rectangle(10, 10, 100, 100);

            //绘制矩形
            g.DrawRectangle(new Pen(Color.DarkGreen, 2), rectangle);
            //左上角写字：原始矩形
            g.DrawString("原始矩形", new Font("微软雅黑", 10), new SolidBrush(Color.DarkGreen), new Point(10, 10));

            //取四象限坐标系的中心点
            Point center = new Point(this.panel1.Width / 2, this.panel1.Height / 2);

            //TranslateTransform:平移变换,将坐标系原点平移到指定的位置
            g.TranslateTransform(center.X, center.Y);

            //绘制矩形
            g.DrawRectangle(new Pen(Color.DarkOrange, 2), rectangle);
            //左上角写字：平移后的矩形
            g.DrawString("平移后的矩形", new Font("微软雅黑", 10), new SolidBrush(Color.DarkOrange), new Point(10, 10));




        }

        private void button25_Click(object sender, EventArgs e)
        {
            //RotateTransform:旋转变换,将坐标系原点旋转指定的角度
            g.RotateTransform(45);

            //绘制矩形
            g.DrawRectangle(new Pen(Color.Red, 2), rectangle);
            //左上角写字：旋转后的矩形
            g.DrawString("旋转后的矩形", new Font("微软雅黑", 10), new SolidBrush(Color.Red), new Point(10, 10));
        }
        /// <summary>
        /// 缩放变换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button26_Click(object sender, EventArgs e)
        {
            //ScaleTransform:缩放变换,将坐标系原点缩放指定的倍数
            g.ScaleTransform(2, 1);//X轴放大2倍，Y轴不变

            //绘制矩形
            g.DrawRectangle(new Pen(Color.Blue, 2), rectangle);
            //左上角写字：缩放后的矩形
            g.DrawString("缩放后的矩形", new Font("微软雅黑", 10), new SolidBrush(Color.Blue), new Point(10, 10));
        }
    }
}
